package org.boot.ssm.wxshop.service.impl;

import org.boot.ssm.wxshop.common.impl.BaseServiceImpl;
import org.boot.ssm.wxshop.domain.CarouselImg;
import org.boot.ssm.wxshop.exception.BusinessException;
import org.boot.ssm.wxshop.mapper.CarouselImgMapper;
import org.boot.ssm.wxshop.service.CarouselService;
import org.boot.ssm.wxshop.web.common.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: wxshop
 * @description: 轮播图业务实现类
 * @author: Mr.Wang
 * @create: 2018-03-17 12:54
 **/
@Service
public class CarouselServiceImpl extends BaseServiceImpl<CarouselImg> implements CarouselService {

    @Autowired
    private CarouselImgMapper carouselImgMapper;

    @Override
    /*****************************
     * @Description: 显示所有轮播图
     * @Param: []
     * @return: java.util.List<org.boot.ssm.wxshop.domain.CarouselImg>
     * @Author: 刘涛
     * @Date: 2018/3/17
     *******************************/
    public List<CarouselImg> selectAllAndGoods() {
        return carouselImgMapper.selectCarouselAndGoods();
    }

    @Override
    /*****************************
     * @Description: 根据ID查询轮播图
     * @Param: [id]
     * @return: org.boot.ssm.wxshop.domain.CarouselImg
     * @Author: 刘涛
     * @Date: 2018/3/17
     *******************************/
    public CarouselImg selectAllAndGoodsById(Integer id) {
        return carouselImgMapper.selectCarouselAndGoodsById(id);
    }

    @Override
    /*****************************
     * @Description: 添加或修改
     * @Param: [carouselImg]
     * @return: org.boot.ssm.wxshop.web.common.JsonResult
     * @Author: 刘涛
     * @Date: 2018/3/17
     *******************************/
    public JsonResult addOrUpdate(CarouselImg carouselImg) {
        Integer isSuccess;
        if(carouselImg.getId()!=null){
            isSuccess =  this.updateNoNull(carouselImg);
            if(isSuccess==1){
                return new JsonResult(200,"修改成功");
            }else {
                throw new BusinessException("修改失败");
            }
        }else {
            CarouselImg countCarouselImg = new CarouselImg();
            countCarouselImg.setIsDelete(1);
            Integer count = this.queryCount(countCarouselImg);
            if(count>=5){
                throw new BusinessException("轮播图已达上限,请删除后再进行添加");
            }else {
                isSuccess = this.saveNoNull(carouselImg);
                if(isSuccess==1){
                    return new JsonResult(200,"添加成功");
                }else {
                    throw new BusinessException("添加失败");
                }
            }

        }
    }

    @Override
    /*****************************
    * @Description: 获取轮播图条数
    * @Param: [] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/17 
    *******************************/
    public JsonResult countCarousel() {
        CarouselImg carouselImg = new CarouselImg();
        carouselImg.setIsDelete(1);
        Integer count = this.queryCount(carouselImg);
        if(count>=5){
            throw new BusinessException("轮播图已达上限,请删除后再进行添加");
        }else {
            return new JsonResult(200,null);
        }
    }

    @Override
    /*****************************
    * @Description: 逻辑删除
    * @Param: [carouselImg] 
    * @return: org.boot.ssm.wxshop.web.common.JsonResult 
    * @Author: 刘涛 
    * @Date: 2018/3/17 
    *******************************/
    public JsonResult deleteCarousel(CarouselImg carouselImg) {
        Integer isDelete;
        carouselImg.setIsDelete(0);
        isDelete = this.updateDelete(carouselImg);
        if (isDelete == 1) {
            return new JsonResult(200, "删除成功");
        } else {
            throw new BusinessException("删除失败");
        }
    }
}
